<template>
  <div class="xtx-carousel" @mouseenter="close" @mouseleave="start">
    <!-- 图片 -->
    <ul class="carousel-body">
      <li
        v-for="(item, index) in sliders"
        :key="item.id"
        class="carousel-item "
        :class="{ fade: activeIndex === index }"
      >
        <RouterLink :to="item.hrefUrl">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <!-- 上一张 -->
    <a href="javascript:;" class="carousel-btn prev" @click="pre"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <!-- 下一张 -->
    <a href="javascript:;" class="carousel-btn next" @click="next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <!-- 轮播图 示意点 -->
    <div class="carousel-indicator">
      <!-- 数组有几个 就生成几个点  基于数字的循环 i是从1开始的-->
      <span
        v-for="i in sliders.length"
        :key="i"
        :class="{ active: i - 1 === activeIndex }"
      ></span>
    </div>
  </div>
</template>

<script>
import { ref ,unmounted} from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 轮播图数据
    sliders: Array,
    autoplay: {
      type: Boolean,
      default: true
    },
    timeId:String
  },
  // 参数1:props 就是props的值
  // props是一个对象 包含了 props中的所有数据
  setup (props) {
    // 下标 索引
    const activeIndex = ref(0)
    let timeId = null
    // console.log('props:', props)
    //开启定时
   const start = ()=>{
        if (props.autoplay === true) {
      // 开启轮播
      clearInterval(timeId)
      timeId=setInterval(() => {
        // 基本类型数据的修改 要通过.value
        activeIndex.value++
        if (activeIndex.value > props.sliders.length - 1) {
          activeIndex.value = 0
        }
      }, 1000)
    }
   }
   //关闭定时器
    const close = ()=>{
        if(timeId){
            clearInterval(timeId)
        }
    }
    start()
    //上一页
    const pre = ()=>{
        if(activeIndex.value>=props.sliders.length-1){
            activeIndex.value=0
        }else{
             activeIndex.value++
        }
    }
    //下一页
    const next = ()=>{
        if(activeIndex.value<=0){
            activeIndex.value=props.sliders.length-1
        }else{
             activeIndex.value--
        }
    }
    // onunmounted(()=>{
    //     close()
    // })
    // 返回 activeIndex
    return {
      activeIndex,
      close,
      start,
      pre,
      next
    }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel {
  width: 1240px;
  height: 500px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 98;
  // 箭头 错开左侧分类
  .carousel-btn.prev {
    left: 270px;
  }
  // 小点 错开左侧分类
  .carousel-indicator {
    padding-left: 250px;
  }
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 270px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
